package com.game;

import com.baidu.bjf.remoting.protobuf.ProtobufIDLGenerator;
import com.game.message.jpb.SuperJPB;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by legend9999 on 2016/6/5.
 */
public class Main {
	public static void main(String[] args) throws Exception {
		List<Integer> a = new ArrayList<>();
		for (int i = 0; i < 100; i++) {
			a.add(i);
		}
		//genPBFile();
	}


	/**
	 * 生成pb文件
	 *
	 * @throws Exception
	 */
	public static void genPBFile() throws Exception {
		String path = "message-bean\\src\\main\\resources\\protobuf\\";
		FileUtils.cleanDirectory(new File(path));
		ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
		Map<String, SuperJPB> beansOfType = ac.getBeansOfType(SuperJPB.class);
		for (SuperJPB superJPB : beansOfType.values()) {
			String content = ProtobufIDLGenerator.getIDL(superJPB.getClass());
			String fullName = superJPB.getClass().getName();
			String[] split = StringUtils.split(fullName, ".");
			String dicName = split[3];
			if (!new File(path + dicName).exists()) {
				new File(path + dicName).mkdir();
			}
			String fileName = dicName + File.separator + superJPB.getClass().getSimpleName() + ".proto";
			FileUtils.write(new File(path + fileName), content);
		}
	}
}
